

	var changeArray =  new Array();
	var isChange = false;






	function loaddata(){
		var allFlag = $("#allFlag").val();
		var pno = $("#pagenum").val();
		var month=$("#month").val();
		var bonus=$("#bonus").val();
		var dept_ID=$("#dept_ID").val()=="null"?null:$("#dept_ID").val();
		var score = $("#score").val();
		var emp_id = $("#emp_id").val();
		var url = pms_url + "wh/performance/getwhgroups";
		var params = {month:month,emp_id:emp_id,pno:pno,allFlag:allFlag,dept_ID:dept_ID};
		var obj = load(url,params);	
		var wh = obj.groups;
		$("#whgroupslist").find("tbody").html("");
		$.each(wh, function(index, p) {
		var tr ="<tr role='row'>"+
		"<td>"+(index+1)+"</td>"+
		"<td>"+((p.department_name=="模一仓储课")?"M1W":(p.department_name=="成品仓储课")?"FGW":"RMW")+"</td>"+
		"<td>"+p.emp_id+"</td>"+ 
		"<td>"+p.emp_name+"</td>"+
		"<td align='center'>"+((p.label==null)?"":p.label)+"</td></tr>";
		$("#whgroupslist").find("tbody").append(tr);
	 });
		
	
	}



	
	
	
	
	function save(){
		
		if(isChange){
			
			var a = new Array();
			var month=$("#month").val();
			var table = document.getElementById("whgroupslist");
			changeArray.sort();
			var newArray = new Array();
			for(var i = 0; i < changeArray.length; i++){
				if(i > 0){
					if(changeArray[i] != changeArray[i-1])
						newArray.push(changeArray[i]);
				}else{
					newArray.push(changeArray[i]);
				}
			}
			
			 
			$('table tr').each(function(i){
				 
				if($.inArray(i, newArray) != -1){
					var tablelist = new Object();
					tablelist.month = month;
					tablelist.employee_id = $(this).children("td:eq(2)").text();
					tablelist.employee_name = $(this).children("td:eq(3)").text();
					tablelist.group = $(this).children("td:eq(4)").text();
					
					a.push(tablelist);
					
				}
				
			});
			
			var obj =JSON.stringify(a);
			
			var aurl = pms_url + "wh/performance/savegroups";
			$.ajax({  
		        type : "post",  
		        url : aurl,  
		        data : {param:obj},
		        dataType : "json", 
		        async : false,  
		        beforeSend: function () {  
        	        $("#btnSave").attr({ disabled: "disabled" });
        	        MaskUtil.mask("正在保存中..."); 
        	        
        	    },  
        	    
        	    complete: function () {  
	            	$("#btnSave").removeAttr("disabled");  
	            	MaskUtil.unmask();   
	            },  
	            
		        success : function(data){  
		           data = eval(data);  
			   		if (data.err_msg=="success") {
			   			if(data.result == "0"){
			   				alert("保存成功");
			   				loaddata();
			   			}else{
			   				alert("error!");
			   			}
					}else {
						alert("error!");
					}
		        }  
		    });
			
			

			
		
		}else{
			alert("内容无改动，无需保存！");
			return;
		}
		
		isChange = false;
		
	}
	



$(function () { 
	loaddata();
	
	//查询
	$('#btnSearch').on('click', function () {
		$("#pagenum").val(1);
		loaddata();
		
	});
	
	//重置
	$("#btnReset").on('click', function () {
		$("#searchform input").val("");
		$("#searchform select").val("");
	});	

	
	
	//导出
	$("#btnExport").on('click', function () {
		$("#whgroupslist").tableExport({
		    headings: true, 
		    footers: true, 
		    formats: ["xls"],
		    fileName: "data",
		    bootstrap: true,
		    position: "bottom",
		    ignoreRows: null,
		    ignoreCols: null
		});   

	});	
	
	
	//保存
	$("#btnSave").on('click', function () {
		save();
	});	
	
	
	
	$(document).on("click","td",function(){
		
		if($(this).children("select").length > 0) 
			   return false;
		
		var td = $(this);
		
		
		
		var tdindex = td.parents("tr").find("td").index($(this));
		if(tdindex == 4){
			var trindex = td.parents("tr").index() + 1;
			
			changeArray.push(trindex);
			
			var td = $(this);  
		    //保存初始值  
		    var tdText = $(this).text();  
		    //给td设置宽度和给input设置宽度并赋值  
		    var select = $("<select><option value='339'>原料</option>" +
		    					   "<option value='340'>包材</option>" +
		    					   "<option value='341'>面板</option>" +
		    					   "<option value='342'>模组</option>" +
		    					   "<option value='343'>包装</option>" +
		    					   "<option value='344'>成品</option></select>");
		    td.text("");
		    
		    
		    select 
			   .width(td.width()-60)                   //设置文本框宽度与td相同 
			   .height(td.height()+2) 
			   .val(tdText) 
			   .appendTo(td)                         //把创建的文本框插入到tdObj子节点的最后
			   .trigger("focus")                        //用trigger方法触发事件 
			   .trigger("select"); 
		    
		    //失去焦点的时候重新赋值  
		    var inputDom = $(this).find("select");  
		    inputDom.blur(function(){  
		    		
		    	var error = false;
		    	
		    	var newText = $(this).find("option:selected").text();  
		    	
		    	
		    	
		       
		        $(this).remove();  
		        
		        if(error){
		        	td.text(tdText);
		        }else{
		        	td.text(newText);
		        }
		        
		        if(newText != tdText){
			    	isChange = true;
			    }
		        
		    });
		}
	   
	    
	    
	});
	
	
	
	
});